package pl.gpms.server.persistance.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
//import org.hibernate.Transaction;

import pl.gpms.client.dto.DefectDictDTO;
import pl.gpms.server.persistance.map.DefectDict;
import pl.gpms.server.persistance.tools.HibernateUtil;

public class DefectDictDao implements Serializable {

	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	public List<String> getIDefectList() {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		List<String> defectNames = new ArrayList<String>();
		List<DefectDict> defectList = null;
		Transaction tx = session.beginTransaction();
		
		try {
			defectList = (ArrayList<DefectDict>) session.createQuery("from DefectDict").list();
		} catch( HibernateException e ) {
			tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
 		
		if( defectList != null ) {
			for ( Iterator<DefectDict> iterator = defectList.iterator(); iterator.hasNext(); ) {			
				//defectNames.add( ( (DefectDict) iterator.next() ).toXMLString() );
			}
		} 
		
		return defectNames;
	}
	
	public DefectDictDTO createDefectDictDTO( DefectDict defectDict ) {
		return new DefectDictDTO( defectDict.getDefectName(), defectDict.getDefectConfidence() );
	}
}
